package com.example.xgakj.newterminal.ui.activities;


import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.example.xgakj.mylibrary.mvp.utils.SharedPreferencesUtil;
import com.example.xgakj.newterminal.R;
import com.example.xgakj.newterminal.adapter.BellChoiceAdapter;
import com.example.xgakj.newterminal.base.BaseActivity;
import com.example.xgakj.newterminal.base.Parameters;
import com.example.xgakj.newterminal.utils.SharedPreferencesManager;
import com.example.xgakj.newterminal.utils.SystemDataManager;
import com.qmuiteam.qmui.util.QMUIViewHelper;
import com.qmuiteam.qmui.widget.QMUITopBar;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @ClassName:SettingSdBellActivity
 * @Description:
 * @author Administrator
 * @date 2015-8-20 上午9:43:52
 *
 */
public class SettingSdBellActivity extends BaseActivity {

	private MediaPlayer player;
	int checkedId;
	int bellType;
	int selectPos;


	ListView list_sd_bell;
	BellChoiceAdapter adapter;

	private QMUITopBar topbar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.set_sd_bell);
		initTopbar();
		init();
		initListener();
	}

	private void init() {

		list_sd_bell = (ListView) findViewById(R.id.list_sd_bell);
		player = new MediaPlayer();
		initSelectState();
		ArrayList<HashMap<String,String>> bells = SystemDataManager.getInstance().getSdBells(this);
	    adapter = new BellChoiceAdapter(this,bells);
		adapter.setCheckedId(checkedId);
		list_sd_bell.setAdapter(adapter);
		list_sd_bell.setSelection(selectPos);
	}
	private void initTopbar() {
		topbar = (QMUITopBar) findViewById(R.id.topbar);
		topbar.setTitle("Sd铃声");
		topbar.addLeftBackImageButton().setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		topbar.addRightTextButton("保存", QMUIViewHelper.generateViewId()).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(checkedId==-1){
					Toast.makeText(SettingSdBellActivity.this, "未选择铃声", Toast.LENGTH_SHORT).show();
				}else{
					SharedPreferencesManager.getInstance().putASdBellId(checkedId, mContex);
					SharedPreferencesManager.getInstance().putABellSelectPos(selectPos, mContex);
					SharedPreferencesManager.getInstance().putABellType(SharedPreferencesManager.TYPE_BELL_SD, mContex);
					String path=SystemDataManager.getInstance().findSdBellById(getApplicationContext(),checkedId).get("path");
					SharedPreferencesUtil.setString(mContex, Parameters.MUSIC,path);
					SharedPreferencesManager.getInstance().putABellType(SharedPreferencesManager.TYPE_BELL_SD, mContex);
					Intent i = new Intent();
					i.setAction(Parameters.ACTION_CHANGEBELL);
					sendBroadcast(i);
					SettingSdBellActivity.this.finish();
				}
			}
		});
	}
	
	private void initListener() {
		list_sd_bell.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				HashMap<String,String> data = (HashMap<String, String>) adapter.getItem(arg2);
				int id = Integer.parseInt(data.get("bellId"));
				checkedId = id;
				selectPos = arg2;
				adapter.setCheckedId(id);
				adapter.notifyDataSetChanged();
				playMusic(checkedId);
			}
			
		});

	}

	public void initSelectState(){
		selectPos = SharedPreferencesManager.getInstance().getABellSelectPos(this);
		bellType = SharedPreferencesManager.getInstance().getABellType(this);
		checkedId = SharedPreferencesManager.getInstance().getASdBellId(this);
		if(bellType==SharedPreferencesManager.TYPE_BELL_SYS){
			checkedId = -1;
			selectPos = 1;
		}

	}
	
	public void playMusic(int bellId){
		
		try {
			player.reset();
			bellType = SharedPreferencesManager.getInstance().getCBellType(mContex);
			HashMap<String,String> data;
			data = SystemDataManager.getInstance().findSdBellById(mContex, bellId);
			String path = data.get("path");
			if(null==path||"".equals(path)){
				
			}else{
				player.setDataSource(path);
				player.prepare();
				player.start();
			}
			
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		player.stop();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		player.stop();
		player.release();
	}

}
